﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ERP.Manager.BusinessServices;
using ERP.Manager.DataEntity;
using ERP.Manager.Controllers.CustomAttributes;
namespace ERP.Manager.Controllers
{
    [CheckLoginAttribute]
    public class ECS_categoryController : Controller
    {
        private readonly ECS_categoryinfoService ecs_categoryinfoservice;
        private readonly ECS_goods_typeinfoService ecs_goods_typeinfoservice;
        private readonly ECS_attributeinfoService ecs_attributeinfoservice;
        public ECS_categoryController() { }
        public ECS_categoryController(ECS_categoryinfoService ecs_categoryinfoservice, ECS_goods_typeinfoService ecs_goods_typeinfoservice, ECS_attributeinfoService ecs_attributeinfoservice)
        {
            this.ecs_categoryinfoservice = ecs_categoryinfoservice;
            this.ecs_goods_typeinfoservice = ecs_goods_typeinfoservice;
            this.ecs_attributeinfoservice = ecs_attributeinfoservice;
        }

        #region 显示商品分类页面
        public ActionResult ShowECS_category(int? page)
        {
            int pagecount = page ?? 1;
            List<ECS_categoryinfo> list = ecs_categoryinfoservice.all_ecs_categoryinfo_page(pagecount);
            ViewData["ecs_categoryinfo"] = list;
            ViewData["ecs_categoryinfo_count"] = ecs_categoryinfoservice.all_ecs_categoryinfo_count();
            return View();
        }
        #endregion

        #region 添加商品分类页面
        public ActionResult AddECS_category()
        {
            List<ECS_goods_typeinfo> list = ecs_goods_typeinfoservice.all_ecs_goods_typeinfo_cat_name();
            ViewData["ecs_goods_typeinfo"] = list;
            return View();
        }
        #endregion

        #region Ajax添加商品分类页面
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxAddECS_category(string cat_name, string keywords, string cat_desc, string measure_unit, string show_in_nav, string style, string is_show, string grade, string filter_attr_group)
        {
            try
            {
                ECS_categoryinfo category = new ECS_categoryinfo();
                category.cat_name = cat_name;
                category.keywords = keywords;
                category.cat_desc = cat_desc;
                category.parent_id = 0;
                category.sort_order = 50;
                category.template_file = "";
                category.measure_unit = measure_unit;
                category.show_in_nav = int.Parse(show_in_nav);
                category.style = style;
                category.is_show = int.Parse(is_show);
                category.grade = grade == "" ? 0 : int.Parse(grade);
                category.filter_attr = filter_attr_group;
                int rowcount = ecs_categoryinfoservice.add_ecs_categoryinfo(category);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region 修改商品分类页面
        public ActionResult UpdateECS_category(int cat_id)
        {
            //用商品分类编号查询商品分类详细信息
            ECS_categoryinfo category = ecs_categoryinfoservice.detail_ecs_categoryinfo(cat_id);
            ViewData["ecs_categoryinfo"] = category;

            //查询商品类型的名称
            List<ECS_goods_typeinfo> list = ecs_goods_typeinfoservice.all_ecs_goods_typeinfo_cat_name();
            ViewData["ecs_goods_typeinfo"] = list;
            ECS_attributeinfo attribute = ecs_attributeinfoservice.detail_ecs_attributeinfo(int.Parse(category.filter_attr.ToString()));
            if (attribute.attr_name == null)
            {
                List<ECS_attributeinfo> attributelist = ecs_attributeinfoservice.all_ecs_attributeinfo_page(attribute.ecsgoodstypeinfo.cat_id, 1);
                ViewData["ecs_attribute"] = attributelist;
            }
            else
            {
                List<ECS_attributeinfo> attributelist = ecs_attributeinfoservice.all_ecs_attributeinfo_page(attribute.ecsgoodstypeinfo.cat_id, 1);
                ViewData["ecs_attribute"] = attributelist;
            }
            return View();
        }
        #endregion

        #region  Ajax修改商品分类页面
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxUpdateECS_category(string category_cat_id, string cat_name, string keywords, string cat_desc, string measure_unit, string show_in_nav, string style, string is_show, string grade, string filter_attr_group)
        {
            try
            {
                ECS_categoryinfo category = new ECS_categoryinfo();
                category.cat_id = int.Parse(category_cat_id);
                category.cat_name = cat_name;
                category.keywords = keywords;
                category.cat_desc = cat_desc;
                category.measure_unit = measure_unit;
                category.show_in_nav = int.Parse(show_in_nav);
                category.style = style;
                category.is_show = int.Parse(is_show);
                category.grade = grade == "" ? 0 : int.Parse(grade);
                category.filter_attr = filter_attr_group;
                int rowcount = ecs_categoryinfoservice.update_ecs_categoryinfo(category);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region 转移商品页面
        public ActionResult zhuanyishangping(string cat_id)
        {
            ViewData["ecs_category_cat_id"] = cat_id.ToString();
            List<ECS_categoryinfo> ecs_category = ecs_categoryinfoservice.all_ecs_categroyinfo();
            ViewData["ecs_category"] = ecs_category;

            return View();
        }
        #endregion


        #region Ajax转移商品页面
        public string Ajaxzhuanyishangping(string cat_id, string new_cat_id)
        {
            try
            {
                int category_cat_id = int.Parse(cat_id.ToString());
                int category_new_cat_id = int.Parse(new_cat_id.ToString());
                int rowcount = ecs_categoryinfoservice.update_ecs_categoryinfo_zhuanyishangping(category_new_cat_id, category_cat_id);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region Ajax查询商品类型下面的商品属性信息
        public JsonResult Ajax_all_ecs_attributeinfo(string cat_id)
        {
            int new_cat_id = int.Parse(cat_id);
            List<ECS_attributeinfo> list = ecs_attributeinfoservice.all_ecs_attributeinfo(new_cat_id);
            return Json(list);
        }
        #endregion

        #region ajax删除商品分类信息的方法
        public string Ajax_delete_ecs_attributeinfo(string cat_id)
        {
            int new_cat_id = int.Parse(cat_id);
            try
            {
                int rowcount = ecs_categoryinfoservice.delete_ecs_categoryinfo(new_cat_id);
                if (rowcount > 0)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion

        #region ajax根据商品分类编号修改商品导航栏的状态
        public string Ajaxupdate_ecs_category_show_in_nav(int cat_id, int show_in_nav)
        {
            try
            {
                int rowcount = ecs_categoryinfoservice.update_ecs_category_show_in_nav(cat_id, show_in_nav);
                if (rowcount > 0)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception)
            {

                return "error";
            }
        }
        #endregion

        #region ajax根据商品分类编号修改商品是否显示的状态
        public string Ajaxupdate_ecs_category_is_show(int cat_id, int is_show)
        {
            try
            {
                int rowcount = ecs_categoryinfoservice.update_ecs_category_is_show(cat_id, is_show);
                if (rowcount > 0)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception)
            {

                return "error";
            }
        }
        #endregion
    }
}
